from django.db import models
from ..models import Product

class WmsReceiptOrder(models.Model):
    id = models.AutoField(primary_key=True)
    order_no = models.CharField(max_length=32, blank=True, null=True, db_comment='入库单号')
    opt_type = models.IntegerField(blank=True, null=True, db_comment='入库类型')
    merchant_id = models.IntegerField(blank=True, null=True, db_comment='供应商')
    biz_order_no = models.CharField(max_length=32, blank=True, null=True, db_comment='业务订单号')
    total_quantity = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='商品总数')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='订单金额')
    order_status = models.IntegerField(blank=True, null=True, db_comment='入库状态')
    warehouse_id = models.IntegerField(blank=True, null=True, db_comment='仓库id')
    remark = models.CharField(max_length=255, blank=True, null=True, db_comment='备注')
    create_by = models.CharField(max_length=64, blank=True, null=True, db_comment='创建人')
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True, db_comment='创建时间')
    update_by = models.CharField(max_length=64, blank=True, null=True, db_comment='修改人')
    update_time = models.DateTimeField(blank=True, null=True, db_comment='修改时间')

    class Meta:
        db_table = 'wms_receipt_order'
        db_table_comment = '入库单'

class WmsItem(models.Model):
    id = models.AutoField(primary_key=True)
    item_code = models.CharField(max_length=20, blank=True, null=True, db_comment='编号')
    item_name = models.CharField(max_length=60, db_comment='名称')
    item_category = models.CharField(max_length=20, blank=True, null=True, db_comment='分类')
    unit = models.CharField(max_length=20, blank=True, null=True, db_comment='单位类别')
    item_brand = models.IntegerField(blank=True, null=True, db_comment='品牌')
    remark = models.CharField(max_length=255, blank=True, null=True, db_comment='备注')
    create_by = models.CharField(max_length=64, blank=True, null=True, db_comment='创建人')
    create_time = models.DateTimeField(blank=True, null=True, db_comment='创建时间')
    update_by = models.CharField(max_length=64, blank=True, null=True, db_comment='修改人')
    update_time = models.DateTimeField(blank=True, null=True, db_comment='修改时间')

    class Meta:
        db_table = 'wms_item'
        db_table_comment = '物料'


class WmsItemSku(models.Model):
    id = models.AutoField(primary_key=True)
    sku_name = models.CharField(max_length=255, blank=True, null=True, db_comment='规格名称')
    item = models.ForeignKey(to='WmsItem',
                                      on_delete=models.CASCADE,
                                      db_comment='物料ID',
                                      db_index=True,
                                      to_field='id')
    barcode = models.CharField(max_length=64, blank=True, null=True, db_comment='条码')
    sku_code = models.CharField(max_length=64, blank=True, null=True, db_comment='编码')
    length = models.DecimalField(max_digits=10, decimal_places=1, blank=True, null=True, db_comment='长，单位cm')
    width = models.DecimalField(max_digits=10, decimal_places=1, blank=True, null=True, db_comment='宽，单位cm')
    height = models.DecimalField(max_digits=10, decimal_places=1, blank=True, null=True, db_comment='高，单位cm')
    gross_weight = models.DecimalField(max_digits=10, decimal_places=3, blank=True, null=True, db_comment='毛重，单位kg')
    net_weight = models.DecimalField(max_digits=10, decimal_places=3, blank=True, null=True, db_comment='净重，单位kg')
    cost_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='成本价（元）')
    selling_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='销售价（元）')
    create_by = models.CharField(max_length=64, blank=True, null=True, db_comment='创建人')
    create_time = models.DateTimeField(blank=True, null=True, db_comment='创建时间')
    update_by = models.CharField(max_length=64, blank=True, null=True, db_comment='修改人')
    update_time = models.DateTimeField(blank=True, null=True, db_comment='修改时间')

    class Meta:
        db_table = 'wms_item_sku'
        db_table_comment = 'sku信息'

class WmsReceiptOrderDetail(models.Model):
    id = models.AutoField(primary_key=True)
    order = models.ForeignKey(to='WmsReceiptOrder',
                                      on_delete=models.CASCADE,
                                      db_comment='ID',
                                      db_index=True,
                                      to_field='id',
                                      related_name='details')
    sku = models.ForeignKey(to='WmsItemSku',
                                      on_delete=models.DO_NOTHING,
                                      db_comment='订单ID',
                                      db_index=True,
                                      to_field='id')
    quantity = models.DecimalField(max_digits=20, decimal_places=2, blank=True, null=True, db_comment='入库数量')
    amount = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='金额')
    warehouse_id = models.IntegerField(blank=True, null=True, db_comment='所属仓库')
    remark = models.CharField(max_length=255, blank=True, null=True, db_comment='备注')
    create_by = models.CharField(max_length=64, blank=True, null=True, db_comment='创建人')
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True, db_comment='创建时间')
    update_by = models.CharField(max_length=64, blank=True, null=True, db_comment='修改人')
    update_time = models.DateTimeField(blank=True, null=True, db_comment='修改时间')

    class Meta:
        db_table = 'wms_receipt_order_detail'
        db_table_comment = '入库单详情'


class WmsShipmentOrder(models.Model):
    id = models.AutoField(primary_key=True)
    order_no = models.CharField(max_length=22, blank=True, null=True, db_comment='出库单号，系统自动生成')
    opt_type = models.IntegerField(blank=True, null=True, db_comment='出库类型')
    biz_order_no = models.CharField(max_length=50, blank=True, null=True, db_comment='业务订单号')
    merchant_id = models.IntegerField(blank=True, null=True, db_comment='客户')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='总金额')
    total_quantity = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='总数量')
    order_status = models.IntegerField(blank=True, null=True, db_comment='出库单状态')
    warehouse_id = models.IntegerField(blank=True, null=True, db_comment='仓库id')
    remark = models.CharField(max_length=255, blank=True, null=True, db_comment='备注')
    create_by = models.CharField(max_length=64, blank=True, null=True, db_comment='创建人')
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True, db_comment='创建时间')
    update_by = models.CharField(max_length=64, blank=True, null=True, db_comment='修改人')
    update_time = models.DateTimeField(blank=True, null=True, db_comment='修改时间')

    class Meta:
        db_table = 'wms_shipment_order'
        db_table_comment = '出库单'


class WmsShipmentOrderDetail(models.Model):
    id = models.AutoField(primary_key=True)
    order_id = models.IntegerField(blank=True, null=True, db_comment='出库单')
    warehouse_id = models.IntegerField(blank=True, null=True, db_comment='所属仓库')
    sku_id = models.IntegerField(blank=True, null=True, db_comment='规格id')
    quantity = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='数量')
    amount = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='金额')
    remark = models.CharField(max_length=255, blank=True, null=True, db_comment='备注')
    create_by = models.CharField(max_length=64, blank=True, null=True, db_comment='创建人')
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True, db_comment='创建时间')
    update_by = models.CharField(max_length=64, blank=True, null=True, db_comment='修改人')
    update_time = models.DateTimeField(blank=True, null=True, db_comment='修改时间')

    class Meta:
        db_table = 'wms_shipment_order_detail'
        db_table_comment = '出库单详情'

class WmsWarehouse(models.Model):
    id = models.AutoField(primary_key=True)
    warehouse_code = models.CharField(max_length=20, blank=True, null=True, db_comment='编号')
    warehouse_name = models.CharField(max_length=50, db_comment='名称')
    remark = models.CharField(max_length=255, blank=True, null=True, db_comment='备注')
    order_num = models.IntegerField(blank=True, null=True, db_comment='排序')
    create_by = models.CharField(max_length=64, blank=True, null=True, db_comment='创建人')
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True, db_comment='创建时间')
    update_by = models.CharField(max_length=64, blank=True, null=True, db_comment='修改人')
    update_time = models.DateTimeField(blank=True, null=True, db_comment='修改时间')

    class Meta:
        db_table = 'wms_warehouse'
        db_table_comment = '仓库'

class WmsInventory(models.Model):
    id = models.AutoField(primary_key=True)
    sku = models.ForeignKey(to='WmsItemSku',
                                      on_delete=models.CASCADE,
                                      db_comment='订单ID',
                                      db_index=True,
                                      to_field='id')
    product = models.ForeignKey(to='Product',
                                      on_delete=models.DO_NOTHING,
                                      db_comment='产品ID',
                                      db_index=True,
                                      to_field='product_id')
    warehouse_id = models.IntegerField(blank=True, null=True, db_comment='所属仓库')
    quantity = models.DecimalField(max_digits=20, decimal_places=2, blank=True, null=True, db_comment='库存')
    remark = models.CharField(max_length=255, blank=True, null=True, db_comment='备注')
    create_by = models.CharField(max_length=64, blank=True, null=True, db_comment='创建人')
    create_time = models.DateTimeField(blank=True, null=True, db_comment='创建时间')
    update_by = models.CharField(max_length=64, blank=True, null=True, db_comment='修改人')
    update_time = models.DateTimeField(blank=True, null=True, db_comment='修改时间')

    class Meta:
        db_table = 'wms_inventory'
        db_table_comment = '库存表'
        constraints = [
            models.UniqueConstraint(
                fields=['product', 'warehouse_id'],
                name='unique_product_warehouse_id'
            )
        ]
